package javagprssrv.Serial;

import java.util.Enumeration;
import java.util.Properties;
import javagprssrv.JGS_Param;
import javax.comm.CommPortIdentifier;
import java.util.Vector;
import javagprssrv.Common.INI;

public class SerialDetect
{
    public void DetectCOM() {
        java.lang.String CK = JGS_Param.properties.getProperty("可用COM").trim();
        final java.util.Vector CKs = new java.util.Vector();
        if (CK.length() > 0) {
            while (CK.length() > 0) {
                if (CK.indexOf(",") > -1) {
                    try {
                        final int intCK = java.lang.Integer.valueOf(CK.substring(0, CK.indexOf(",")).trim());
                        CKs.add(java.lang.Integer.valueOf(intCK).toString());
                    }
                    catch (java.lang.Exception ex) {}
                    CK = CK.substring(CK.indexOf(",") + 1, CK.length());
                }
                else {
                    try {
                        final int intCK = java.lang.Integer.valueOf(CK.trim());
                        CKs.add(java.lang.Integer.valueOf(intCK).toString());
                    }
                    catch (java.lang.Exception ex2) {}
                    CK = "";
                }
            }
        }
        final java.util.Vector SysCKs = new java.util.Vector();
        final java.lang.String strCOM = "";
        try {
            final java.util.Enumeration portList = javax.comm.CommPortIdentifier.getPortIdentifiers();
            java.lang.Boolean isFindCOM = false;
            while (portList.hasMoreElements()) {
                final javax.comm.CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
                if (!portId.getName().toUpperCase().substring(0, 3).equals("COM")) {
                    continue;
                }
                isFindCOM = true;
                SysCKs.add(portId.getName().substring(3, portId.getName().length()));
            }
            if (!isFindCOM) {
                javagprssrv.JGS_Param.myLog.ShowLog("(警告) 未发现可用串口！！！", true, true);
            }
        }
        catch (java.lang.Exception ex3) {}
        for (int i = 0; i < CKs.size(); ++i) {
            final java.lang.String strCK = java.lang.String.valueOf(CKs.elementAt(i)).toString().trim();
            final int intCK2 = java.lang.Integer.valueOf(strCK);
            boolean isSysCKExist = false;
            for (int j = 0; j < SysCKs.size(); ++j) {
                final java.lang.String strSysCK = java.lang.String.valueOf(SysCKs.elementAt(j)).toString();
                final int intSysCK = java.lang.Integer.valueOf(strSysCK);
                if (intSysCK == intCK2) {
                    isSysCKExist = true;
                    break;
                }
            }
            if (isSysCKExist) {
                try {
                    final javagprssrv.Serial.SerialBean SB = new javagprssrv.Serial.SerialBean(intCK2);
                    SB.Initialize();
                    SB.WritePort("ATI\r\n");
                }
                catch (java.lang.Exception e) {
                    javagprssrv.JGS_Param.myLog.ShowLog("(错误) 串口" + strCK + "(COM" + strCK + ")已经被打开，请检查有关应用程序 ！！！", true, true);
                }
            }
            else {
                javagprssrv.JGS_Param.myLog.ShowLog("(警告) 系统中并不存在串口" + strCK + "(COM" + strCK + ")", true, true);
            }
        }
    }
}
